<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>ajax</title>
    <script src="js/jquery.min.js"></script>
    <script type="application/javascript">//type可有可无
    alert("rw");
    $(function () {
        $("#a").click(function () {
            alert("lljla");
            //发送ajax请求，格式必须正确
            $.ajax({
                url: "${pageContext.request.contextPath}/demo/ajax",
                type: "post",
               // contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: function (data) {
                    var tBody = $("#t_body");
                    var str = "";
                    if (data != null) {
                        //循环取出,注意字符串的拼接
                        for (var i = 0; i < data.length; i++) {
                            str += "<tr>";
                            str += "<td>" + data[i].username + "</td>";
                            str += "<td>" + data[i].password + "</td>";
                            str += "<td>" + data[i].sex + "</td>";
                            str += "</tr>";
                        }
                        tBody.html(str);
                    }
                }
                //data:{"username":"jack","password":"ll","sex":"男"}
            });
        });
        $("#a4").click(function () {
            var val1 = $("#a1").val();//获取value属性的值
            var val2 = $("#a2").val();
            var val3 = $("#a3").val();
            alert(val1);
            $.ajax({
                url: "demo/a1",
                type: "post",
                //contentType: "application/json;charset=UTF-8",
                dataType: "json",
                data: {username: val1, password: val2, sex: val3},
                success: function (data) {
                    alert("lll")
                }
            })
        })
    })
    </script>
</head>
<body>
<h2>Hello World!</h2>
<br/>
<form action="demo/a" method="post"enctype="multipart/form-data">
    <input type="text" name="username" id="a1">
    <input type="text" name="password" id="a2">
    <input type="text" name="sex" id="a3">
    <input type="button" value="按钮" id="a4">
</form>
<a href="demo/b/你好">testVoid</a>
<button id="a">按钮</button>
<table border="1px solid" cellspacing="0">
    <tr>
        <td>用户名</td>
        <td>密码</td>
        <td>性别</td>
    </tr>
    <tbody id="t_body">

    </tbody>
</table>
</body>
</html>
